package com.play.ballen.home.ui

import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.Button
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.play.ballen.databinding.ActivityNoticeListBinding
import com.play.ballen.home.adapter.NoticeListAdapter
import com.play.ballen.home.vm.ArticleVM
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.yuven.appframework.base.BaseStdTitleActivity
import com.yuven.baselib.utils.ResourceUtil
import com.yuven.baselib.widget.MultiStateView

class NoticeListActivity : BaseStdTitleActivity<ArticleVM, ActivityNoticeListBinding>() {

    private val adapter: NoticeListAdapter by lazy {
        NoticeListAdapter(arrayListOf())
    }

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, NoticeListActivity::class.java)
            context.startActivity(starter)
        }
    }

    override fun viewModelClass(): Class<ArticleVM> {
        return ArticleVM::class.java
    }

    override fun contentLayoutId(): Int {
        return R.layout.activity_notice_list
    }

    override fun getToolBarTitle(): String {
        return ResourceUtil.getString(R.string.notice_title)
    }

    override fun initView() {
        super.initView()
        dataBinding.msvLayout.findViewById<Button>(R.id.btnTryAgain).setOnClickListener {
            dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_LOADING
            viewModel.getArticleList()
        }
    }

    override fun initObserver() {
        super.initObserver()
        viewModel.actionGetArticleListFailure.observe(this) {
            dataBinding.refreshLayout.finishRefresh()
            dataBinding.refreshLayout.finishLoadMore()
            if (viewModel.page == 1) {
                dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_ERROR
            } else {
                ToastUtils.show(ResourceUtil.getString(R.string.load_fail))
            }
        }

        viewModel.listArticle.observe(this) {
            dataBinding.refreshLayout.finishRefresh()
            dataBinding.refreshLayout.finishLoadMore()

            if (!it.isNullOrEmpty()) {
                if (viewModel.page == 1) {
                    adapter.setList(it)

                    dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_CONTENT
                } else {
                    adapter.addData(it)
                }
                viewModel.page++
            } else {
                //no data
                if (viewModel.page == 1) {
                    dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_EMPTY
                } else {
                    ToastUtils.show(ResourceUtil.getString(R.string.load_end))
                }
            }
        }
    }

    override fun initData() {
        dataBinding.refreshLayout.setEnableLoadMore(true)
        dataBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.page = 1
                viewModel.getArticleList()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.getArticleList()
            }
        })

        dataBinding.rv.layoutManager = LinearLayoutManager(mContext)
        dataBinding.rv.adapter = adapter
        adapter.setOnItemClickListener { a, view, position ->
            val info = adapter.data[position]
            NoticeDetailActivity.start(this@NoticeListActivity, info)
        }

        dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_LOADING
        viewModel.page = 1
        viewModel.getArticleList()
    }

}